/*************************************************************************
	> File Name: testdatastruct.c
	> Author: Wang Bo
	> Mail: 1276106221@qq.com
	> Created Time: 2020年12月03日 星期四 19时03分34秒
 ************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "testdatastruct.h"


void initrand(); __attribute__ ((constructor))
void initrand() {
    srand((time(0)));
}

void testList() {
    printf(PPP("测试链表\n"));
    List *head = getNewList();
    for (int i = 0; i < max_op; ++i) {
        int val = rand() % 100;
        int oper = rand() % 4;
        int ind = rand() % (head->length + 2) - 1;
        switch (oper) {
            case 0:
            case 1: {
                printf(YYY(">>>插入%d到链表的%d位置\n"), val, ind);
                printf("插入%s\n", insertListNode(head, ind, val) ? GGG("成功") : RRR("失败"));
            } break;
            case 2:
            case 3: {
                printf(BBB("<<<删除链表%d位置的数据\n"), ind);
                printf("删除%s\n", eraseListNode(head, ind) ? GGG("成功") : RRR("失败"));
            } break;
        }
        outputList(head);
    }
    printf("就地翻转链表\n");
    reverseListAt(head);
    outputList(head);
    clearList(head);
    printf("测试完毕\n");
}

void testVector() {
    printf(PPP("测试向量Vector\n"));
    Vector *v = getNewVector(max_op);
    printf("初始化Vector\n");
    for (int i = 0; i < max_op; ++i) {
        pushBackVector(v, i + 1);
    }
    outputVector(v);
    printf("现在Vector的大小为 %d\n", getVectorSize(v));
    for (int i = 0; i < max_op; ++i) {
        int val = rand() % 100;
        int oper = rand() % 4;
        int ind = rand() % (v->ind + 2) - 1;
        switch (oper) {
            case 0:
            case 1: {
                printf(YYY("<<<删除Vector的下标%d的元素\n"), ind);
                printf("移除%s\n", eraseVectorAt(v, ind) ? GGG("成功") : RRR("失败"));
            } break;
            case 2:
            case 3: {
                printf(BBB(">>>插入%d到Vector的下标%d中\n"), val, ind);
                printf("插入%s\n", insertVectorAt(v, ind, val) ? GGG("成功") : RRR("失败"));
            } break;
        }
        outputVector(v);
    }
    printf("现在Vector的大小为 %d\n", getVectorSize(v));
    clearVector(v);
    printf(PPP("测试完毕\n"));
}

void testBinaryTree() {
    printf(PPP("测试二叉搜索树\n"));
    BSTree *tree = getNewTree();
    for (int i = 0; i < max_op * 2; ++i) {
        int val = rand() % 100;
        printf(YYY("向树中插入%d\n"), val);
        insertRoot(tree, val);
    }
    generalize(tree);
    clearTree(tree);
}

void testQueue() {
    printf(PPP("测试循环队列\n"));    
    Queue *q = initQueue(max_op / 2);
    for (int i = 0; i < max_op * 2; ++i) {
        int val = rand() % 100;
        int oper = rand() % 5;
        switch (oper) {
            case 0:
            case 1:
            case 2: {
                printf(BBB(">>>向队列中插入%d\n"), val);
                printf("插入%s\n", pushQueue(q, val) ? GGG("成功") : RRR("失败"));
            } break;
            case 3:
            case 4: {
                printf(YYY("<<<从队列中弹出元素\n"));
                printf("弹出%s\n", popQueue(q) ? GGG("成功") : RRR("失败"));
            } break;
        }
        outputQueue(q);
    }
    printf("当前队列大小为 %d\n", q->size);
    clearQueue(q);
    printf(PPP("测试完毕\n"));
}

void testPriorityQueue() {
    printf(PPP("测试优先队列\n"));
    PriQue *q = initPriQue(max_op);
    for (int i = 0; i < max_op; ++i) {
        int val = rand() % 100;
        printf(">>>插入"YYY("%d")"到优先队列中\n", val);
        pushPriQue(q, val);
    }
    printf(BBB("开始出队<<<\n"));
    for (int i = 0; i < max_op; ++i) {
        printf("%d ", topPriQue(q));
        popPriQue(q);
    }
    clearPriQue(q);
    printf(PPP("\n测试完毕\n"));
}

void testUnionSet() {
    printf(PPP("开始测试并查集\n"));
    UnionSet *u = initUnionSet(max_op * 10);
    for (int i = 0; i < max_op * 3; ++i) {
        int a = rand() % 80 + 1;
        int b = rand() % 80 + 20;
        printf("%d号节点与%d号节点相连\n", a, b);
        merge(u, a, b);
    }
    printf(BBB("查询并查集\n"));
    for (int i = 1; i <= max_op * 10; ++i) {
        if (find(u, i) == i) continue;
        printf("节点%d的父节点为%d\n", i, find(u, i));
    }
    clearUnionSet(u);
    printf(PPP("测试完毕\n"));
}


